<?php
session_start ();
require_once './php_lib/conexion.php';
require_once('./php_lib/funciones.php');

$lista_sucursal = obtener_sucursales($db_zapatos);	
$lista_personal = obtener_personal($db_zapatos);



if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$query_Color = "SELECT * FROM Color";
$Color = mysql_query($query_Color, $db_zapatos) or die(mysql_error());
$row_Color = mysql_fetch_assoc($Color);
$totalRows_Color = mysql_num_rows($Color);

$query_Talla = "SELECT * FROM Talla";
$Talla = mysql_query($query_Talla, $db_zapatos) or die(mysql_error());
$row_Talla = mysql_fetch_assoc($Talla);
$totalRows_Talla = mysql_num_rows($Talla);

$query_Modelo = "SELECT mod_id, mod_descripcion FROM Modelo";
$Modelo = mysql_query($query_Modelo, $db_zapatos) or die(mysql_error());
$row_Modelo = mysql_fetch_assoc($Modelo);
$totalRows_Modelo = mysql_num_rows($Modelo);

?>
<?php
if(isset($_POST['prod']) 
or (isset($_POST['valor']) && $_POST['valor']!="!=0") 
or (isset($_POST['color']) && $_POST['color']!="!=0")
or (isset($_POST['talla']) && $_POST['talla']!="!=0")
or (isset($_POST['modelo']) && $_POST['modelo']!="!=0")){
	if(isset($_POST['prod'])){
		$slike = "AND prod_id=".$_POST['prod'];
	}
	else{
		$slike="";
		if(isset($_POST['valor'])){
			$slike .= " AND Producto.prod_valor ".$_POST['valor'];
		}
		if(isset($_POST['color'])){
			$slike .= " AND Color.col_id ".$_POST['color'];
		}
		if(isset($_POST['talla'])){
			$slike .= " AND Talla.talla_id ".$_POST['talla'];
		}
		if(isset($_POST['modelo'])){
			$slike .= " AND Modelo.mod_id ".$_POST['modelo'];
		}
		
	}
$query = "SELECT prod_id,prod_valor,col_descripcion,talla_num,mod_descripcion
FROM Producto,Color,Talla,Modelo
WHERE Producto.col_id = Color.col_id
AND Producto.col_id = Color.col_id
AND Producto.talla_id = Talla.talla_id
AND Producto.mod_id = Modelo.mod_id
".$slike;
// 
	$queEmp = mysql_query($query, $db_zapatos)or die(mysql_error());
	$rsEmp  = mysql_fetch_assoc($queEmp);
	}
?>
<link href="css/estilos.css" rel="stylesheet" type="text/css" />

<h3><em> ID del producto </em>
</h3>
<form id="form1" name="form1" method="post" action=""> 
  <label for="prod"></label>
    <input type="text" name="prod" id="prod" />
    <input type="button" name="button1" id="button1" value="Buscar"/>
  </p>
</form>

<p>
  <?php if(isset($rsEmp) && $rsEmp!=null){?>
  </p>
  
   <div>
    <label for="sucursal">Sucursal</label>
    <select name="sucursal" id="sucursal">
	<option value="0">Seleccione una sucursal</option>
	<? foreach( $lista_sucursal as $suc_key => $suc_val) { 
		 echo sprintf("<option value=\"%d\">%s</option>", $suc_key, $suc_val);
	} ?>
    </select>
  </div>
  
   <div>
    <label for="personal">Personal</label>
    <select name="personal" id="personal">
	<option value="0">Seleccione un Personal</option>
	<? foreach( $lista_personal as $per_key => $per_val) { 
		 echo sprintf("<option value=\"%d\">%s</option>", $per_key, $per_val);
	} ?>
    </select>
  </div>
  
<table width="503">
  <tr>
    <th width="73">Producto</th>
    <th width="49">Valor</th>
    <th width="49">Color</th>
    <th width="53">Talla</th>
    <th width="61">Modelo</th>
    <th width="70"> Cantidad  </th>
    <th width="116"> añadir a venta </th>
  </tr>
  <?php do { ?>
  <tr>
    <td><?php echo $rsEmp['prod_id']; ?></td>
    <td><?php echo $rsEmp['prod_valor']; ?></td>
    <td><?php echo $rsEmp['col_descripcion']; ?></td>
    <td><?php echo $rsEmp['talla_num']; ?></td>
    <td><?php echo $rsEmp['mod_descripcion']; ?></td>
    <th><input name="cantidad" type="text" width="30" /> </th>
  </tr>
  <?php }while ($rsEmp = mysql_fetch_assoc($queEmp));?>
</table>
  <?php }?>

<script language="javascript">
$('#button1').click(function() {
	// Enviamos el formulario usando AJAX
	$.ajax({
	type: 'POST',
	url: 'realizar_venta.php',
	data: $('#form1').serialize(),
	// Mostramos un mensaje con la respuesta de PHP
	success: function(data) {
		$('#mainDiv').html(data);
	}})        
});

$('#button2').click(function() {
	// Enviamos el formulario usando AJAX
	$.ajax({
	type: 'POST',
	url: 'realizar_venta.php',
	data: $('#form2').serialize(),
	// Mostramos un mensaje con la respuesta de PHP
	success: function(data) {
		$('#mainDiv').html(data);
	}})        
});

</script>